﻿using System;
using System.Collections.Generic;
using System.Linq;
using XcoAppSpaces.Core.Ports.Utils;

namespace XcoAppSpaces.Core.Ports
{
	/// <summary>
	/// A ConcurrentExclusiveGroup allows defining a group of ports, where some allow concurrent processing,
	/// while others only allow that their items are processed exclusively over all ports in the group (meaning
	/// no other item can be processed in the meantime).
	/// </summary>
	public class ConcurrentExclusiveGroup
	{
		private readonly AsyncReaderWriterLock _asyncLock = new AsyncReaderWriterLock();

		/// <summary>
		/// Adds a concurrent port to the group.
		/// </summary>
		/// <param name="port">The port to be added.</param>
		public void AddConcurrentPort(Port port)
		{
			port.MapToConcurrentExclusiveGroup(_asyncLock, false);
		}

		/// <summary>
		/// Adds an exclusive port to the group.
		/// </summary>
		/// <param name="port">The port to be added.</param>
		public void AddExclusivePort(Port port)
		{
			port.MapToConcurrentExclusiveGroup(_asyncLock, true);
		}
	}
}
